package top.lv666.graduation_design_serve.service;

import com.vdurmont.emoji.EmojiParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.lv666.graduation_design_serve.mapper.BlogMapper;
import top.lv666.graduation_design_serve.mapper.TopicMapper;
import top.lv666.graduation_design_serve.pojo.*;
import top.lv666.graduation_design_serve.util.Tools;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class TopicServiceImpl implements TopicService {
    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private BlogMapper blogMapper;
    //避免变量重复声明，节约性能
    private Map<String, Object> parmMap = new HashMap<>();

    /**
     * 获取当前搜索热门话题
     */
    @Override
    public List<Topic> getHotSearchTopicList(int getPage, int getNumber) {
        // 设置必要参数
        getPage = (getPage - 1) * getNumber;
        return topicMapper.getHotSearchTopicList(getPage, getNumber);
    }

    /**
     * @apiNote 获取当前信息
     */
    @Override
    public Topic getTopicById(String id) {
        Topic topic = topicMapper.getTopicById(id);
        int seeNumber = topic.getSeeNumber();
        double topiceNumber = topic.getTopicNumber();
        String createTime = topic.getCreateTime();
        double searchNumber = topic.getSearchNumber();
        parmMap.clear();
        parmMap.put("id", id);
        parmMap.put("seeNumber", ++seeNumber);
        parmMap.put("hotSearch", Tools.getHotSearch(searchNumber, seeNumber, topiceNumber, createTime));
        topicMapper.modifyTopic(parmMap);
        return topic;
    }

    /**
     * @apiNote 获取个税
     */
    @Override
    public int getCount() {
        return topicMapper.getCount();
    }

    /**
     * @apiNote 生产话题下的附属博客
     */
    @Override
    public void createTopicBlog(Map<String, Object> map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        map.remove("request");
        Tools.isMapNull(map);
        Tools.isLogin(request);

        String blogID = Tools.getUUID();
        String userID = Tools.getUserId(request);
        String refwordUrl = "NULL";
        String blogUpTime = Tools.getTime();
        map.put("blogID", blogID);
        map.put("userID", userID);
        map.put("refwordUrl", refwordUrl);
        map.put("blogUpTime", blogUpTime);
        map.put("blogContent", EmojiParser.parseToAliases((String) map.get("blogContent")));
        topicMapper.createTopicBlog(map);
    }

    /**
     * @apiNote 获取该主题下的博文
     */
    @Override
    public List<Blog> getTopicBlogHot(Map<String, Object> map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        map.remove("request");
        Tools.isMapNull(map);
        // 设置必要参数
        int getPage = (int) map.get("getPage");
        getPage = (getPage - 1) * (int) map.get("getNumber");
        map.put("getPage", getPage);

        // 返回数据
        List<Blog> blogList = topicMapper.queryTopicBlogHot(map);
        // 首先获取userId(用户登陆的话)
        // 确认登录
        if (request.getHeader("Authorization") != null && !(request.getHeader("Authorization").equals(""))) {
            Tools.isLogin(request);
            parmMap.clear();
            //设置当前的操作用户
            parmMap.put("userId", Tools.getUserId(request));
            // 查询该用户点赞的博文
            List<BlogLike> likeInfo = blogMapper.SelectUserIsLike(parmMap);
            // 查询该用户的转发博文
            List<BlogRef> blogRefs = blogMapper.SelectBlogRef(parmMap);
            // 查询用户的关注列表
            List<Attention> attentions = blogMapper.SelectAttention(parmMap);
            return Tools.isBlogOpt(request, blogList, likeInfo, blogRefs, attentions);
        } else {
            return Tools.isBlogOpt(request, blogList);
        }
    }

    /**
     * @apiNote 创建话题
     */
    @Override
    public void createTopic(Map<String, Object> map) {
        HttpServletRequest request = (HttpServletRequest) map.get("request");
        map.remove("request");
        Tools.isMapNull(map);
        Tools.isLogin(request);
        String userId = Tools.getUserId(request);
        String topicId = Tools.getUUID();
        String time = Tools.getTime();
        map.put("id", topicId);
        map.put("uploadUser", userId);
        map.put("createTime", time);
        topicMapper.createTopic(map);
    }

    /**
     * @apiNote 获取列表
     */
    @Override
    public List<Topic> getTopicList(int getPage, int getNumber) {
        getPage = (getPage - 1) * getNumber;
        return topicMapper.getTopicList(getPage, getNumber);
    }
}
